package com.example.shopping.controller;

import com.example.shopping.pojo.Seller;
import com.example.shopping.service.SellerService;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/*
Create by 苍狼 于 2020/1/16
站在巅峰看世界，
落在深渊思人生。
*/
@CrossOrigin
@Controller
//商家管理
public class SellerController {
    @Autowired(required = false)
    private SellerService sellerService;

    //全部查询
    @RequestMapping("/selectSellerList")
    public String selectSellerList(HttpServletRequest request) {
        List<Seller> sellerList = sellerService.selectSellerList();
        request.getSession().setAttribute("sellerList", sellerList);
        return "seller";
    }

    //根据商家名称和公司名称模糊查询
    @RequestMapping("/QueryByLike")
    public String QueryByLike(Seller seller, HttpServletRequest request) {
        if (seller != null && !seller.equals("")) {
            List<Seller> sellerList = sellerService.queryByLike(seller);
            request.getSession().setAttribute("sellerList", sellerList);
            return "seller";
        }
        return "redirect:/selectList";
    }

    //修改
    @RequestMapping("/update")
    public String update(Seller seller) {
        int i = sellerService.update(seller);
        if (i > 0) {
            return "redirect:selectSellerList";
        }
        return "redirect:update";
    }

    //关闭商家店铺
    @RequestMapping("/updateCloseSeller")
    public String updateCloseSeller(int id) {
        sellerService.updateCloseSeller(id);
        return "redirect:selectSellerList";
    }

    //根据商家状态查询
    @RequestMapping("/SellerByLikeStatus")
    public String SellerByLikeStatus(int id, HttpServletRequest request) {
        List<Seller> sellers = sellerService.SellerByLikeStatus(id);
        request.getSession().setAttribute("sellers", sellers);
        return "seller";
    }

    //生成报表
    @RequestMapping("/DownloadExcel")
    public String Excel(int id) {
        /*文件下载*/
        System.out.println(id);
        /*1、查询数据库获取数据*/
        Seller seller = sellerService.SellerById(id);
        /*2、创建文件名*/
        String fileName = seller.getName();
        /*3、设置路径名*/
        StringBuffer filePath = new StringBuffer("D:\\");
        filePath.append(fileName).append(".xlsx");
        System.out.println(filePath);
        /*通过POI生成Excel文件*/
        XSSFWorkbook xssfWorkbook = null;
        /*java7新特性，自动资源回收*/
        try (FileOutputStream fos = new FileOutputStream(filePath.toString())) {
            /*4、在内存中创建一个xlsx文件*/
            xssfWorkbook = new XSSFWorkbook();
            /*5、创建工作簿*/
            XSSFSheet sheet1 = xssfWorkbook.createSheet("sheet1");
            /*6、创建标题行*/
            XSSFRow row1 = sheet1.createRow(0);
            row1.createCell(0).setCellValue("商家编号");
            row1.createCell(1).setCellValue("公司名称");
            row1.createCell(2).setCellValue("店铺名称");
            row1.createCell(3).setCellValue("公司手机");
            row1.createCell(4).setCellValue("公司电话");
            row1.createCell(5).setCellValue("详细地址");
            row1.createCell(6).setCellValue("联系人姓名");
            row1.createCell(7).setCellValue("联系人电话");
            row1.createCell(8).setCellValue("联系人QQ");
            row1.createCell(9).setCellValue("联系人EMAIL");
            row1.createCell(10).setCellValue("营业执照号");
            row1.createCell(11).setCellValue("税务登记证号");
            row1.createCell(12).setCellValue("组织机构代码");
            row1.createCell(13).setCellValue("法定代表人");
            row1.createCell(14).setCellValue("法定代表人身份证");
            row1.createCell(15).setCellValue("开户行账号名称");
            row1.createCell(16).setCellValue("开户行");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:selectSellerList";
    }

}
